﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Winista.Text.HtmlParser;
using Winista.Text.HtmlParser.Lex;
using Winista.Text.HtmlParser.Util;
using Winista.Text.HtmlParser.Tags;
using Winista.Text.HtmlParser.Filters;
using System.Windows.Forms;
using fmtx.Utility;

namespace CollectArticle
{
   public class DownHtml
    {

       public string DownFile(string url,string tagbox,string attr,string attrval,string encode)
       {
           string html = "";
           #region 获得网页的html
           try
           {
               System.Net.WebClient aWebClient = new System.Net.WebClient();
               switch (encode)
               {
                   case "utf-8":
                       aWebClient.Encoding = System.Text.Encoding.UTF8;
                       break;
                   case "gbk":
                       aWebClient.Encoding = System.Text.Encoding.Default;
                       break;
                   default:
                       aWebClient.Encoding = System.Text.Encoding.ASCII;
                       break;
               }
               html = aWebClient.DownloadString(url);
           }
           catch (Exception ex)
           {
               fmtx.Framework.Log.Error(ex);
           }
           #endregion

           #region 分析网页html节点

           return Chaihtml(html, tagbox, attr, attrval);

           #endregion 
       }

       private string Chaihtml(string html,string tagbox,string attr,string attrval)
       {
           Lexer lexer = new Lexer(html);
           Parser parser = new Parser(lexer);
           NodeFilter filter = new TagNameFilter(tagbox);
           NodeList htmlNodes = parser.Parse(filter);
           for (int i = 0; i < htmlNodes.Count; i++)
           {
               var aa = htmlNodes[i].ToHtml();
               if (WebHelper.HasRegex(attr + "=\"" + attrval + "\"", aa))
               {
                   ITag tag = getTag(htmlNodes[i]);
                   string oClass = tag.GetAttribute(attr);
                   if (oClass != null && oClass.Equals(attrval))
                   {
                       return tag.ToHtml();
                   }
                   Chaihtml(htmlNodes[i].Children.ToHtml(), tagbox, attr, attrval);
               }
               
           }
           return "";
       }

       public List<string> ChaiTag(string html, string tag)
       {
           List<string> list = new List<string>();
           Lexer lexer = new Lexer(html);
           Parser parser = new Parser(lexer);
           NodeFilter filter = new TagNameFilter(tag);
           NodeList htmlNodes = parser.Parse(filter);
           for (int i = 0; i < htmlNodes.Count; i++)
           {
               list.Add(htmlNodes[i].ToHtml());
           }
           return list;
       }

       public string ChaiTagfirst(string html, string tag)
       {
           List<string> list = new List<string>();
           Lexer lexer = new Lexer(html);
           Parser parser = new Parser(lexer);
           NodeFilter filter = new TagNameFilter(tag);
           NodeList htmlNodes = parser.Parse(filter);
           if (htmlNodes.Count > 0)
           {
               return htmlNodes[0].ToHtml();
           }
           else {
               return "";
           }
       }

       private ITag getTag(INode node)
       {
           if (node == null)
               return null;
           return node is ITag ? node as ITag : null;
       }
    }
}
